package com.person.jc.service.finance;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BsOrderBillingService extends Service {
	
	public final static BsOrderBillingService me = new BsOrderBillingService();
	
	public List<Record> getList(Map<String, Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t.*,t3.cust_name,t4.user_name,t5.user_name salesman FROM bs_billing t");
		sql.append(" left join bd_customer t3 on t.billing_unit = t3.cust_id ");
		sql.append(" left join ts_user t4 on t.billing_user = t4.user_id ");
		sql.append(" left join ts_user t5 on t.salesman = t5.user_id ");
		sql.append(" where t.billing_date is not null ");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("status")))){
			sql.append(" and t.status = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("billingId")))){
			sql.append(" and t.billing_id = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("billingUser")))){
			sql.append(" and t.billing_user = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("billingUnit")))){
			sql.append(" and t.billing_unit = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and date_format(t.billing_date,'%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endtDate")))){
			sql.append(" and date_format(t.billing_date,'%Y-%m-%d') <= ?");
			params.add(temp);
		}
		sql.append(" order by t.billing_id desc");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void pay(Map<String, Object> map){
		String sql = "update bs_billing set status = 2 where billing_id = ?";
		Db.update(sql,map.get("billingId"));
		super.setCRUD("订单开票", "U", "确认开票："+map.get("billingId"));
	}

	public List<Record> getBillingDetail(String billingId) {
		String sql = "select t.* from bs_billing_detail t where billing_id = ? ";
		return Db.find(sql, billingId);
	}

	public void doTackTickets(Map<String, Object> map) {
		String sql = "update bs_billing set tickets_remark=?,order_code=?,ticket_way=?,ticket_user=?,tickct_date=?,status = 3 where billing_id = ?";
		Db.update(sql,map.get("ticketsRemark"),map.get("orderCode"),map.get("ticketWay"),map.get("ticketUser"),map.get("tickctDate"),map.get("billingId"));
		super.setCRUD("取票", "U", "确认取票："+map.get("billingId"));
		
	}

	public BigDecimal getPaymentAmount(String billingId) {
		String sql = "SELECT SUM(payment_amount) FROM bs_billing_pay_detail WHERE billing_id = ? ";
		return Db.queryBigDecimal(sql, billingId);
	}

	public void doPayBack(Map<String, Object> map) {
		String sql = "update bs_billing set status = ? where billing_id = ?";
		Db.update(sql,map.get("status"),map.get("billingId"));
		super.setCRUD("确认回款", "U", "确认回款："+map.get("billingId"));
		List<Object> params = new ArrayList<Object>();
		String csql = "insert into bs_billing_pay_detail (billing_id, payment_amount,payment_way,payment_date,payment_remark) values (?, ?, ?, ?, ?)";
		params.add(map.get("billingId"));
		params.add(map.get("paymentAmount"));
		params.add(map.get("paymentWay"));
		params.add(map.get("paymentDate"));
		params.add(map.get("paymentRemark"));
		Db.update(csql, Utils.getObjParam(params));
		super.setCRUD("客户单位管理", "C", "新增客户信息："+map.get("custName"));
	}

	public List<Record> getPayBackDetail(String billingId) {
		String sql = "select t.* from bs_billing_pay_detail t where billing_id = ? order by payment_date desc ";
		return Db.find(sql, billingId);
	}

	public List<Record> getUntreatedCount() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t.billing_id FROM bs_billing t WHERE t.status =1");
		return Db.find(sql.toString());
	}

	public List<Record> getIncompleteCount() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t.billing_id FROM bs_billing t WHERE t.status in (2,3,4)");
		return Db.find(sql.toString());
	}

	public List<Record> getUntreatedSCount(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t.billing_id FROM bs_billing t WHERE t.status =1 AND t.billing_user=?");
		return Db.find(sql.toString(),userId);
	}
	
}
